package com.p2p.front.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.base.annotation.LoginNeedless;
import com.p2p.base.model.BaseUser;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.component.MemberSetting;
import com.p2p.front.dal.model.IdCardInfo;
import com.p2p.front.dal.model.MemberPoint;
import com.p2p.front.service.BankCardService;
import com.p2p.front.service.FinanceService;
import com.p2p.front.service.IdCardService;
import com.p2p.front.service.InvestInfoService;
import com.p2p.front.service.IpInfoService;
import com.p2p.front.service.MemberPointService;
import com.p2p.front.service.UserInfoAuthService;
import com.p2p.trans.client.facade.AccountQueryFacade;
import com.p2p.trans.client.model.AccountVO;
import com.p2p.trans.client.result.AccountQueryResult;
import com.p2p.user.enums.UserAuthName;

/**
 * @author ruiqi
 *
 */
@Controller
@RequestMapping("/home/*")
public class UserHomeController extends BaseController {

	@Resource
	AccountQueryFacade remoteAccountQueryFacade;
	@Resource 
	com.p2p.front.service.MemberPointService MemberPointService;
	@Resource
	UserInfoAuthService userInfoAuthService;
	@Resource
	private FinanceService financeService;
	@Resource
	private MemberPointService memberPointService;
	@Resource
	private MemberSetting memberSetting;
	@Resource
	private InvestInfoService investInfoService;

	@Resource
	private IdCardService idCardService;
	@Resource
	private BankCardService bankCardService;
	
	@Resource
	private IpInfoService ipInfoService;
	
	
	@RequestMapping("/home/index")
	public String index(Model model) {

		BaseUser user =getCurrentUser();
		model.addAttribute("user", user);
		String lastLoginZone = ipInfoService.getZoneByIpStr(user.getLastLoginIp());
		model.addAttribute("lastLoginZone", lastLoginZone);
		AccountQueryResult accountQueryResult  = remoteAccountQueryFacade.getAccountByUserId(user.getId());
		if (accountQueryResult.isSuccess()) {
			AccountVO accountVO = accountQueryResult.getAccountVO();
			model.addAttribute("accountVO", accountVO);
		}
		Integer uid=super.getCurrentUser().getIntId();
		//获取总的资金记录用于页面展示
		Map<String,Object> finance = financeService.myFinance(uid);
		model.addAttribute("finance", finance);
		//得到首页中需要显示的投资信息
		List<UserAuthName> authNameList = new ArrayList<UserAuthName>();
		authNameList.add(UserAuthName.AUTH_MOBILE);
		authNameList.add(UserAuthName.AUTH_EMAIL);
		authNameList.add(UserAuthName.AUTH_REALNAME);
		Map<Integer, Character> resultMap = userInfoAuthService.getAuthByEnumList(user.getId(), authNameList);
		model.addAttribute("realNameStatus", resultMap.get(UserAuthName.AUTH_REALNAME.getIndex()).toString());
		model.addAttribute("mobileStatus", resultMap.get(UserAuthName.AUTH_MOBILE.getIndex()).toString());
		model.addAttribute("emailStatus", resultMap.get(UserAuthName.AUTH_EMAIL.getIndex()).toString());
		MemberPoint memberPoint = memberPointService.getUserMemberPoint(user.getId());
		Integer nextPoint = memberSetting.getMemberLevelPoint(memberPoint.getLevel()+1);
		if(nextPoint==null){
			nextPoint = memberSetting.getMemberLevelPoint(memberPoint.getLevel());
		}
		Integer bankCardCount = bankCardService.countBankCard(user.getId());
		model.addAttribute("bankCardCount",bankCardCount);
		memberPoint.setNextLevelPoint(nextPoint);
		model.addAttribute("memberPoint", memberPoint);
		
		IdCardInfo idCardInfo = idCardService.getAuthing(user.getId());
		if (idCardInfo != null) {
			model.addAttribute("authIdCardStatus", idCardInfo.getStatus());
		} else {
			model.addAttribute("authIdCardStatus", -1);
		}
		
		return "home/index";
	}
	
	@LoginNeedless
	@RequestMapping("/home/bizError")
	public String bizError(Model model, String errorMessage) {
		model.addAttribute("errorMessage", errorMessage);
		return "home/bizError";
	}

}
